package com.camemax.controller;

import com.camemax.operators.LoginFailedProcess;
import com.camemax.operators.LoginFailedProcessModify;
import com.camemax.pojo.LoginEvent;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.net.URL;
import java.time.Duration;

/*
* 改良版LoginFailedDetect， 在原有基础上修改逻辑判断为两次登录为"fail"的数据流间隔判断。
* */
public class LoginFailedDetect_Modify {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment().setParallelism(1);

        URL resource = LoginFailedDetect.class.getResource("/LoginLog.csv");
        env.readTextFile(resource.getPath())
                .map( line -> {
                    String[] fields = line.split(",");
                    return new LoginEvent(
                            Long.valueOf(fields[0]),
                            fields[1],
                            fields[2],
                            Long.parseLong(fields[3])
                    );
                })
                .assignTimestampsAndWatermarks(WatermarkStrategy.<LoginEvent>forBoundedOutOfOrderness(Duration.ofSeconds(10))
                        .withTimestampAssigner((SerializableTimestampAssigner<LoginEvent>) (pojo, defaultTime) -> pojo.getLoginDate() * 1000L))
                .keyBy(LoginEvent::getUserId)
                .process(new LoginFailedProcessModify())
                .print();


        env.execute();
    }
}
